package com.wisc.application;

import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Level;

import com.google.protobuf.ServiceException;

import edu.wisc.hevea.constants.ApplicationConstants;
import edu.wisc.hevea.exception.BrokerUnavailableException;
import edu.wisc.hevea.producer.Producer;

public class ProducerApplication {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		if (args.length < 1) {
			System.err
					.println(" Please enter the producer's host name!");
			return;
		}
		
		org.apache.log4j.BasicConfigurator.configure();
		org.apache.log4j.Logger.getRootLogger().setLevel(Level.INFO);
		
		String appID = "APP_ID_1";
		short replication = 2;
		List<String> dcsHosted = Arrays.asList(new String[] {
				ApplicationConstants.VIRGINIA_DC, ApplicationConstants.IAD_DC,
				ApplicationConstants.DUBLIN_DC });
		ApplicationPolicy appPolicy = new ApplicationPolicy(appID, replication, dcsHosted);

		String hostname = args[0]; // ApplicationConstants.VIRGINIA_DC;
		Producer producer = new Producer(hostname, appPolicy);
		
		String topic1 = "TOPIC_1";
		
		while(true) {
		    try {
		    	producer.sendMessage(topic1, "The current time is " + (new Date()).toLocaleString());
				// Thread.currentThread();
				Thread.sleep(3 * 1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (BrokerUnavailableException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ServiceException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}	
		}
		
	}

}
